/*
 * TextPane.java  0.4.0 / Oct 27, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.graphics.Graphics;
import com.cute4j.swing.properties.TextPaneProperties;
import com.cute4j.swing.text.StyledDocument;

/**
 * Text Pane Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.1.0
 */
public class TextPane extends TextComponent implements TextPaneProperties {

  // TODO: add scrollbar support

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Private Fields. -------------------------------------------------------------

  private boolean wrapText;
  private SwingGraphics _viewGraphics;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new TextPane object.
   */
  public TextPane() {
    this((String)null);
  }

  /**
   * Constructs new TextPane object.
   *
   * @param text the text
   */
  public TextPane(String text) {
    super(new StyledDocument(text));
    documentOwner = true;
    wrapText = true;
  }

  /**
   * Constructs new TextPane object.
   *
   * @param document the document
   */
  public TextPane(StyledDocument document) {
    super(document);
    wrapText = true;
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns document.
   *
   * @return document
   */
  public StyledDocument getDocument() {
    return (StyledDocument)document;
  }

  /**
   * Sets document.
   *
   * @param document the document
   */
  public void setDocument(StyledDocument document) { // NOPMD
    super.setDocument(document);
  }

  /**
   * Returns <tt>true</tt> if the text area is wrapping text.
   *
   * @return <tt>true</tt> if the text area is wrapping text
   */
  public boolean isWrapText() {
    return wrapText;
  }

  /**
   * Sets wrap text status.
   *
   * @param wrapText if <tt>true</tt> the text area will wrap text
   */
  public void setWrapText(boolean wrapText) {
    boolean oldWrapText = this.wrapText;
    if (oldWrapText != wrapText) {
      disposedCheck();
      _setWrapText(_delegate, wrapText);
      this.wrapText = wrapText;
      firePropertyChange(PROPERTY_WRAP_TEXT, oldWrapText, wrapText);
    }
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Paints view.
   *
   * @param g the graphics object
   * @param x the x-coordinate of the view
   * @param y the y-coordinate of the view
   * @param width the width of the view
   * @param height the height of the view
   *
   * @throws IllegalStateException if method is called outside paint event
   *         handling
   */
  protected void paintComponentView(Graphics g, int x, int y, int width,
                                    int height) {
    SwingGraphics graphics = _viewGraphics;
    if ((graphics != null) && (graphics == g)) { // NOPMD
      _paintComponentView(_delegate, graphics._graphicsPointer);
    }
  }

// Package-Private Methods. ----------------------------------------------------

  void _paintView(long graphicsPointer, int x, int y, int width, int height) {
    SwingGraphics graphics = new SwingGraphics(graphicsPointer);
    _viewGraphics = graphics;
    paintComponentView(graphics, x, y, width, height);
    _viewGraphics = null;
    graphics.dispose();
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _setWrapText(long delegate, boolean wrapText);

  native void _paintComponentView(long delegate, long graphicsPointer);

// Private Static Native Methods. ----------------------------------------------

  private static native void _initialize();

}
